package cn.iocoder.yudao.module.mqtt.controller.admin.config;

import cn.iocoder.yudao.module.mqtt.service.config.MqttConfig2Service;
import cn.iocoder.yudao.module.mqtt.service.mqtt.MqttConfigService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mqtt.controller.admin.config.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.config.MqttConfig2DO;

@Tag(name = "管理后台 - MQTT配置")
@RestController
@RequestMapping("/mqtt/config")
@Validated
public class MqttConfig2Controller {

    @Resource
    private MqttConfig2Service mqttConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建MQTT配置")
    @PreAuthorize("@ss.hasPermission('mqtt:config:create')")
    public CommonResult<Long> createConfig(@Valid @RequestBody ConfigSaveReqVO createReqVO) {
        createReqVO.setIsOnline("1");
        return success(mqttConfigService.createConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新MQTT配置")
    @PreAuthorize("@ss.hasPermission('mqtt:config:update')")
    public CommonResult<Boolean> updateConfig(@Valid @RequestBody ConfigSaveReqVO updateReqVO) {
        mqttConfigService.updateConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除MQTT配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mqtt:config:delete')")
    public CommonResult<Boolean> deleteConfig(@RequestParam("id") Long id) {
        mqttConfigService.deleteConfig(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除MQTT配置")
                @PreAuthorize("@ss.hasPermission('mqtt:config:delete')")
    public CommonResult<Boolean> deleteConfigList(@RequestParam("ids") List<Long> ids) {
        mqttConfigService.deleteConfigListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得MQTT配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mqtt:config:query')")
    public CommonResult<ConfigRespVO> getConfig(@RequestParam("id") Long id) {
        MqttConfig2DO config = mqttConfigService.getConfig(id);
        return success(BeanUtils.toBean(config, ConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得MQTT配置分页")
    @PreAuthorize("@ss.hasPermission('mqtt:config:query')")
    public CommonResult<PageResult<ConfigRespVO>> getConfigPage(@Valid ConfigPageReqVO pageReqVO) {
        PageResult<MqttConfig2DO> pageResult = mqttConfigService.getConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出MQTT配置 Excel")
    @PreAuthorize("@ss.hasPermission('mqtt:config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfigExcel(@Valid ConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MqttConfig2DO> list = mqttConfigService.getConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "MQTT配置.xls", "数据", ConfigRespVO.class,
                        BeanUtils.toBean(list, ConfigRespVO.class));
    }

}